/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/*************************************************************************
 * File: epoll.h
 *************************************************************************
 *\author: starlove
 *************************************************************************/

#ifndef __MLSE_PLATFORM_WIN__

#ifndef __MLSE_NETWORK_EPOLL_H__
#define __MLSE_NETWORK_EPOLL_H__

#include <network/event.h>
#include <network/network.h>

class CEpollNetwork : public INetwork
{
public:
    bool Init();
    bool Destory();
    static unsigned int GetSocketCount();
    static SOCKET CreateTCPSocket();
    static void CloseSocket(SOCKET s);
    static void SetSocketOpt( SOCKET s, int linger_flag = 0 );
    static sockaddr *BuildSockAddr( const char *szAddr, unsigned int uPort );
};
extern volatile int g_nSocketCount;

class CEpollSocket : public ISocket
{
public:
    CEpollSocket();
    ~CEpollSocket();
    bool Init(CEventDispatcher *ep);
    void BindProcessor(IEventProcessor *pp);
    void BindIO(SOCKET s);
    bool Destory();
    void Accept(SOCKET s_listen, IEventProcessor *pp);
    SOCKET Connect(const char *szAddr, unsigned int uPort);
    SOCKET Listen(unsigned int uPort, const char *szAddr = NULL);
    void GetSockAddr(LPEVENT ev, sockaddr *pSelfAddr, sockaddr *pRemoteAddr);
    void Close(SOCKET s);
    void Close(LPEVENT ev);
    int Send(SOCKET &s, void *pData, unsigned int nSize);
    int Recv(SOCKET &s, void *pBuffer, unsigned int nBufferSize);
    int IOSend(SOCKET s, IPeer *pPeer, IEventProcessor *pp = NULL);
    int IORecv(SOCKET s, IPeer *pPeer, IEventProcessor *pp = NULL);
    IEventIO *GetEventIO(){ return m_ep->GetIO(); };
protected:
private:
    IEventProcessor *m_pp;
    CEventDispatcher *m_ep;
};

#endif //
#endif // __MLSE_PLATFORM_WIN__
